package com.lianbang.pharmacy.module.main

import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentStatePagerAdapter
import androidx.viewpager.widget.ViewPager
import cn.ubest.mvvmcore.base.BaseFragment
import cn.ubest.mvvmcore.utils.ResourcesUtils
import com.flyco.roundview.RoundTextView
import com.gyf.barlibrary.ImmersionBar
import com.lianbang.pharmacy.R
import com.lianbang.pharmacy.databinding.FragmentHomeBinding
import com.lianbang.pharmacy.module.medicine.category.ProductCategoryActivity
import com.lianbang.pharmacy.vm.HomeVM
import com.youth.banner.adapter.BannerImageAdapter
import com.youth.banner.holder.BannerImageHolder
import com.youth.banner.indicator.RectangleIndicator
import kotlinx.android.synthetic.main.fragment_home.*


class HomeFragment : BaseFragment<HomeVM, FragmentHomeBinding>() {

    companion object {
        fun newInstance(): HomeFragment {
            val fragment = HomeFragment()
            return fragment
        }
    }

    val mFragmentList = mutableListOf<HomePagerFragment>()

    override fun initLayout(): Int {
        return R.layout.fragment_home
    }

    override fun initData(arguments: Bundle?) {

    }

    override fun initView(view: View) {
        initFragmentList()
        btn_product_category.setOnClickListener {
            ProductCategoryActivity.start(activity)
        }
    }

    override fun loadData() {

    }


    private fun initFragmentList() {
        mFragmentList.add(HomePagerFragment.newInstance())
        mFragmentList.add(HomePagerFragment.newInstance())
        mFragmentList.add(HomePagerFragment.newInstance())
        mFragmentList.add(HomePagerFragment.newInstance())
        activity?.also {
            val adapter = object : BannerImageAdapter<String>(mutableListOf("", "", "", "")) {
                override fun onBindView(
                    holder: BannerImageHolder,
                    data: String,
                    position: Int,
                    size: Int
                ) {
                    //图片加载自己实现
                    val color = when {
                        position % 3 == 0 -> {
                            R.color.colorPrimary
                        }
                        position % 3 == 1 -> {
                            R.color.black
                        }
                        else -> {
                            R.color.C_FFD214
                        }
                    }
                    holder.imageView.setImageResource(color)
                }
            }
            mBinding?.banner?.setAdapter(adapter)?.addBannerLifecycleObserver(this)?.indicator =
                RectangleIndicator(it)

            val statusBarHeight = ImmersionBar.getStatusBarHeight(it)
            view_status_bar.layoutParams.height = statusBarHeight
            vp_pager.adapter = object : FragmentStatePagerAdapter(
                it.supportFragmentManager,
                BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT
            ) {
                override fun getItem(position: Int): Fragment {
                    return mFragmentList[position]
                }

                override fun getCount(): Int {
                    return mFragmentList.size
                }
            }
            vp_pager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
                override fun onPageScrollStateChanged(state: Int) {

                }

                override fun onPageScrolled(
                    position: Int,
                    positionOffset: Float,
                    positionOffsetPixels: Int
                ) {

                }

                override fun onPageSelected(position: Int) {
                    updateIndicator(position)
                }
            })
            updateIndicator(0)
        }
    }

    private fun updateIndicator(position: Int) {
        if (mFragmentList.size > 1) {
            fl_indicator.visibility = View.VISIBLE
            val width = ResourcesUtils.getDimens(R.dimen.dp_29) * mFragmentList.size
            fl_indicator.layoutParams.width = width

            val marginStart = position * ResourcesUtils.getDimens(R.dimen.dp_29)
            if (fl_indicator.childCount > 0) {
                fl_indicator.setPadding(marginStart, 0, 0, 0)
            } else {
                val v = RoundTextView(activity)
                v.delegate.backgroundColor = ResourcesUtils.getColor(R.color.colorPrimary)
                v.delegate.cornerRadius = ResourcesUtils.getDimens(R.dimen.dp_3)
                fl_indicator.addView(
                    v, ResourcesUtils.getDimens(R.dimen.dp_29)
                    , ViewGroup.LayoutParams.MATCH_PARENT
                )
                fl_indicator.setPadding(marginStart, 0, 0, 0)
            }
        } else {
            fl_indicator.visibility = View.GONE
        }

    }
}